package com.songcha.module_mine.ui.activity.unregister

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.event.user.UnRegisterEvent
import com.songcha.library_business.provider.ILogoutProvider
import com.songcha.library_business.provider.IUnRegisterProvider
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_network.bean.BaseBean
import org.greenrobot.eventbus.EventBus

class UnRegisterAccountViewModel(app: Application):BaseViewModel<UnRegisterAccountRepository>(app) {
    //59-0
    val verifyCountDown= MutableLiveData(0)
    val sendVerifySuccess= MutableLiveData<Boolean>()
    val unregisterState= MutableLiveData<Boolean>()
    var registerType=1

    fun setCountDown(countDown:Int){
        verifyCountDown.value=countDown
    }

    fun sendVerify(phone:String){
        handleApiDataObserver(repository!!.sendUnRegisterVerify(phone),object :IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                sendVerifySuccess.value=true
            }

            override fun onError(e: Throwable) {
                sendVerifySuccess.value=false
            }

        })
    }

    fun unregisterByWx(code:String){
        handleApiDataObserver(repository!!.unregisterByWx(code),object :IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                unregisterState.value=true
                UserInfoManager.clearLogin()
                ARouter.getInstance().build("/task/DailyTaskController").navigation()?.let {
                    (it as IUnRegisterProvider).onUnRegister()
                }
                EventBus.getDefault().post(UnRegisterEvent())
            }

            override fun onError(e: Throwable) {
                unregisterState.value=false
            }

        })
    }

    fun unregister(phone:String,code:String){
        handleApiDataObserver(repository!!.unregister(phone,code),object :IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                unregisterState.value=true
                UserInfoManager.clearLogin()
                ARouter.getInstance().build("/task/DailyTaskController").navigation()?.let {
                    (it as IUnRegisterProvider).onUnRegister()
                }
                EventBus.getDefault().post(UnRegisterEvent())
            }

            override fun onError(e: Throwable) {
                unregisterState.value=false
            }

        })
    }
}